import { getCssVariableValue } from '@/utils/utils';

// 所有指标配置
const indexMap = {
  contractStatementPower: {
    type: 'line',
    name: '中长期结算曲线',
    color: '#2174FF',
  },
  selfPlan: {
    type: 'line',
    name: '自计划',
    color: '#3CB1FB',
  },
  bilateralClearingPower: {
    type: 'line',
    name: '双边出清',
    color: '#07A872',
  },
  intradayPlan: {
    type: 'line',
    name: '实时计划',
    color: '#D29E08',
  },
  actualPower: {
    type: 'line',
    name: '实时上网scada电力',
    color: '#ED8139',
  },
  dayaheadPowerForecast: {
    type: 'line',
    name: '短期预测',
    color: '#FB6E77',
  },
  intradayPowerForecast: {
    type: 'line',
    name: '超短期预测',
    color: '#FF70CF',
  },
  transprovincialClearingPower: {
    type: 'line',
    name: '跨省区现货出清电力',
    color: '#B382F0',
  },
  realTimeClearingBasis: {
    type: 'line',
    name: '实时出清依据',
    color: '#575FA1',
  },
  dayAheadClearPrice: {
    type: 'dashed',
    name: '日前出清电价',
    color: '#5E92F6',
  },
  realTimeClearPrice: {
    type: 'dashed',
    name: '实时出清电价',
    color: '#E86452',
  },
  dayAheadClearElectricity: {
    type: 'line',
    name: '日前出清功率',
    color: '#5E92F6',
  },
  actualInternetElectricity: {
    type: 'line',
    name: '实际发电功率',
    color: '#07A872',
  },
  realtimeClearingQuantity: {
    type: 'line',
    name: '实时出清功率',
    color: '#E86452',
  },
};
// 指标分类(默认0电力, 1电价)
const gridKeyMap = {
  dayAheadClearPrice: 1,
  realTimeClearPrice: 1,
};

// 不同省份业务区分
const keysMap = {
  // 甘肃
  62: [
    'contractStatementPower',
    'selfPlan',
    'bilateralClearingPower',
    'intradayPlan',
    'actualPower',
    'dayaheadPowerForecast',
    'intradayPowerForecast',
    'transprovincialClearingPower',
    'realTimeClearingBasis',
    'dayAheadClearPrice',
    'realTimeClearPrice',
  ],
  // 山西&其他
  default: [
    'dayAheadClearElectricity',
    'realtimeClearingQuantity',
    'dayAheadClearPrice',
    'realTimeClearPrice',
    'actualInternetElectricity',
  ],
};

const TYPE_KEY = 'type';
const CHART_X_KEY = 'id';
export const COL_WIDTH = 90;
const chartBaseOption = () => {
  return {
    grid: [
      { left: 48, right: 0, top: '6%', height: '38%' },
      { left: 48, right: 0, bottom: 52, top: '58%' },
    ],
    axisPointer: {},
    legend: {
      textStyle: {
        color: getCssVariableValue('--chart-legend-color'),
      },
    },
    tooltip: {},
    xAxis: [
      {
        type: 'category',
        formatter: (v) => v.slice(4),
        gridIndex: 0,
        axisLine: {
          lineStyle: {
            color: getCssVariableValue('--chart-x-line'),
          },
        },
        axisTick: {
          lineStyle: {
            color: getCssVariableValue('--chart-x-line'),
          },
        },
        axisLabel: {
          color: getCssVariableValue('--chart-font-color'),
        },
        axisPointer: {
          type: 'line',
          lineStyle: {
            color: getCssVariableValue('--chart-x-line'),
          },
        },
      },
      {
        type: 'category',
        formatter: (v) => v.slice(4),
        gridIndex: 1,
        axisLabel: { show: false },
        axisTick: { show: false },
        axisLine: {
          lineStyle: {
            color: getCssVariableValue('--chart-x-line'),
          },
        },
        axisTick: {
          lineStyle: {
            color: getCssVariableValue('--chart-x-line'),
          },
        },
        axisLabel: {
          color: getCssVariableValue('--chart-font-color'),
        },
        axisPointer: {
          type: 'line',
          lineStyle: {
            color: getCssVariableValue('--chart-x-line'),
          },
        },
      },
    ],
    yAxis: [
      {
        type: 'value',
        name: 'MW',
        gridIndex: 0,
        nameTextStyle: {
          color: getCssVariableValue('--chart-font-color'),
        },
        splitLine: {
          lineStyle: {
            color: getCssVariableValue('--chart-y-line'),
          },
        },
        axisLabel: {
          color: getCssVariableValue('--chart-font-color'),
        },
      },
      {
        type: 'value',
        name: '元/MWh',
        gridIndex: 1,
        nameTextStyle: {
          color: getCssVariableValue('--chart-font-color'),
        },
        splitLine: {
          lineStyle: {
            color: getCssVariableValue('--chart-y-line'),
          },
        },
        axisLabel: {
          color: getCssVariableValue('--chart-font-color'),
        },
      },
    ],
  };
};
export const fmtDataSource = (_data, _params) => {
  const { provinceId } = _params;
  const keys = (keysMap?.[provinceId] || keysMap.default)?.filter((k) => {
    const config = indexMap?.[k] || '';
    // 黑龙江不展示 实时出清功率&实际发电功率 指标
    return !(provinceId === '23' && config.name === '实际发电功率');
  });
  let dataMap = {}; // 平铺JSON
  let tableData = []; // 表数据:以指标为行,时间为列
  const series = keys
    .map((k, i) => {
      const itemMap = _data?.[k] || {};
      const config = indexMap?.[k] || '';
      let record = { id: `${i}`, [TYPE_KEY]: config?.name || '' };
      for (const date in itemMap) {
        const dataList = itemMap?.[date] || [];
        const rdate = date.replace(/[-]/g, '');
        dataList.map((x) => {
          const datetime = `${rdate} ${x.xAxis.replace(':', '')}`;
          let numVal = Number(x.value);
          if (!!numVal || numVal === 0) {
            numVal = numVal.toFixed(3);
          } else {
            numVal = x.value;
          }
          if (!dataMap[datetime]) {
            // 注意跨年时会有重复问题，so存唯一值，展示再切短
            dataMap[datetime] = { [CHART_X_KEY]: datetime };
          }
          dataMap[datetime][k] = numVal;
          record[datetime] = numVal;
        });
      }
      tableData.push(record);
      const gridIndex = gridKeyMap[k] || 0;
      return {
        ...config,
        props: {
          xAxisIndex: gridIndex,
          yAxisIndex: gridIndex,
        },
      };
    })
    ?.filter((seriesItem) => seriesItem);
  const dataArr = Object.values(dataMap).sort((a, b) => (a[CHART_X_KEY] > b[CHART_X_KEY] ? 1 : -1));
  const tableColumns = [
    { code: TYPE_KEY, name: '类型', width: 130, lock: true },
    ...dataArr.map((x) => ({
      code: x[CHART_X_KEY],
      name: x[CHART_X_KEY].slice(4),
      width: COL_WIDTH,
      align: 'right',
      render: (v) => v || '-',
    })),
  ];
  return {
    chartOption: {
      ...chartBaseOption(),
      series,
      dataset: {
        dimensions: [CHART_X_KEY, ...keys],
        source: dataArr,
      },
    },
    tableColumns,
    tableData,
  };
};
export const exportData = {
  actualPower: '实时上网scada电力',
  bilateralClearingPower: '双边出清',
  contractStatementPower: '中长期结算曲线',
  dayAheadClearPrice: '日前出清电价',
  dayaheadPowerForecast: '短期预测',
  intradayPlan: '实时计划',
  intradayPowerForecast: '超短期预测',
  realTimeClearPrice: '实时出清电价',
  selfPlan: '自计划',
  transprovincialClearingPower: '跨省区现货出清电力',
};

export const exportHead_gansu = [
  `中长期结算曲线`,
  '自计划',
  `双边出清`,
  '实时计划',
  `实时上网scada电力`,
  '短期预测',
  `超短期预测`,
  '跨省区现货出清电力',
  '实时出清依据',
  `日前出清电价`,
  '实时出清电价',
];

//保留小数位数
export const _parseFloat = (val, length) => {
  return parseFloat(parseFloat(val).toFixed(length));
};
